package club.itstudio.IContainer.service;

import club.itstudio.IContainer.entity.Image;
import club.itstudio.IContainer.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class ImageService {
    private JdbcTemplate jdbcTemplate;
    private ImageMapper imageMapper = new ImageMapper();
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean insert(Image image) {
        String sql = "insert into image(image_name, created_time, user_id, description, `open`) " +
                "values(?, ?, ?, ?, ?)";
        try {
            jdbcTemplate.update(sql, image.imageName, image.createdTime, image.userID, image.description, image.open);
        }catch (DataAccessException e) {
            logger.error("error inert new image:", e);
            return false;
        }
        return true;
    }

    public Image getByImageID(int imageID) {
        String sql = "select * from image where id=?";
        Image image = null;
        try {
            image = jdbcTemplate.queryForObject(sql, imageMapper, imageID);
        }catch (DataAccessException e) {
            logger.info("no image with id:" + imageID);
        }
        return image;
    }

    public Image getByImageName(String imageName) {
        String sql = "select * from image where image_name=?";
        Image image = null;
        try {
            image = jdbcTemplate.queryForObject(sql, imageMapper, imageName);
        }catch (DataAccessException e) {
            logger.info("no image with name:" + imageName);
        }
        return image;
    }

    public List<Image> getImageList(int user_id) {
        String sql = "SELECT * FROM image WHERE user_id=? OR `open`=1 ";
        return jdbcTemplate.query(sql, imageMapper, user_id);
    }

    public boolean delete(String imageName) {
        String sql = "delete from image where image_name=?";
        try {
            int count = jdbcTemplate.update(sql, imageName);
            return count == 1;
        }catch (DataAccessException e) {
            logger.info("no such image named:" + imageName,  e);
            return false;
        }
    }
}

class ImageMapper implements RowMapper<Image> {
    @Override
    public Image mapRow(ResultSet resultSet, int i) throws SQLException {
        Image image = new Image();
        image.id = resultSet.getInt("id");
        image.imageName = resultSet.getString("image_name");
        image.createdTime = resultSet.getLong("created_time");
        image.userID = resultSet.getInt("user_id");
        image.description = resultSet.getString("description");
        image.open = resultSet.getInt("open");
        return image;
    }
}
